package cn.stylefeng.guns.modular.robot.cmd.impl;

import cn.stylefeng.guns.modular.robot.cmd.abs.AbstractCmd;
import cn.stylefeng.guns.modular.robot.entity.OrderBatch;
import cn.stylefeng.guns.modular.robot.service.OrderBatchService;
import cn.stylefeng.guns.modular.robot.utils.RobotUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.objects.Update;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
@Transactional
public class CmdHistory extends AbstractCmd {

    @Resource
    private OrderBatchService orderBatchService;
    @Value("${local.url}")
    private String localUrl;

    @Override
    public void exec(Update update, TelegramLongPollingBot bot) {
        String text = update.getMessage().getText();
        if (!getCmdName().equals(text)) {
            return;
        }
        Long chatId = update.getMessage().getChatId();
        List<OrderBatch> orderBatches = orderBatchService.lambdaQuery()
                .eq(OrderBatch::getGroupId, chatId)
                .orderByDesc(OrderBatch::getCreateTime)
                .list();
        StringBuilder sb = new StringBuilder();
        sb.append("总共显示 ").append(orderBatches.size()).append(" 条账单记录。\n\n");
        for (OrderBatch orderBatch : orderBatches) {
            String url = localUrl + "order/indexBatch?batchId=" + orderBatch.getBatchId();
            sb.append("编号：<a href=\"").append(url).append("\">").append(orderBatch.getId()).append("</a>\n");
        }
        RobotUtils.sendText(chatId, sb.toString(), bot);
    }

    @Override
    public String getCmdName() {
        return "历史账单";
    }

    @Override
    public String getDescribe() {
        return "历史账单                   查看历史批次账单\n";
    }
}
